/* util */
import _ from 'lodash';
/* api */
import * as SettingTaskApi from '@src/api/SettingTaskApi';
import { parse } from '@src/util/querystring'
// components
import NoteSetDialog from './components/NoteSetDialog';
import DateSetDialog from './components/DateSetDialog';
import { openAccurateTab } from '@src/util/platform';
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
import i18n from '@src/locales'
export default {
  name: 'task_card_setting',
  data() {
    return {
      cardConfig: {},
      isShowNoteSetDialog:false,
      fieldsList:[],
      loading: false,
      taskCardId:'',
      cardName:'',
      notifyPositionNode:[i18n.t('task.detail.components.startTiming'), i18n.t('task.detail.components.endTiming')]
    }
  },
  mounted(){
    let query = parse(window.location.search) || {};
    this.taskCardId = query.cardId;

    this.getTaskCardNameReq();
    this.initCardView();
  },
  methods: {
    // 计时方式改变
    handleTimingModeChange(){
      if(this.notifyPositionNode.length > 0 ){
        this.cardConfig.notifyPositionNode = this.notifyPositionNode.toString()
      }else{
        this.cardConfig.notifyPositionNode = ''
      }
      this.onSetChange()
    },
    // 工单距离改变
    handleInputChange(value){
      let RegExp = /(^[0-9]([0-9]+)?(\.[0-9]{1,3})?$)/
      if(!RegExp.exec(value)){
        this.cardConfig.notifyDistanceRange = ''
        return this.$platform.notification({ title: i18n.t('task.tip.taskHoursRecordTip1'), type: 'error' })
      }
      if( !Number(value) || Number(value) > 10000 ){
        this.cardConfig.notifyDistanceRange = ''
        return this.$platform.notification({ title: i18n.t('task.tip.taskHoursRecordTip2'), type: 'error' })
      }
      this.cardConfig.notifyDistanceRange = value
      this.onSetChange()
    },
    // 保存用时记录
    onSaveSetSubmit(value, type) {
      this.fieldsList.forEach((field, item )=> {
        if(field.fieldId == value.fieldId ){
          field.displayName = value.displayName;
          field.isNull = value.isNull;
        } 
      })

      this.saveCardConfigReq();
      if(type == 'dateSet'){
        this.$refs.dateSetDialog.onClose();
      }else{
        this.$refs.noteSetDialog.onClose();
      }
    },

    onSetChange() {
      // 如果 计时自动获取用户定位 未开启的话，且位置异常提醒已开启  则把位置异常提醒关闭
      if(!this.cardConfig.autoLocation && this.cardConfig.notifyPositionState){
        this.cardConfig.notifyPositionState = false
      }
      this.saveCardConfigReq();
    },

    // 点击设置
    onSet(setType, field) {
      if(setType == 'noteSet') {
        this.$refs.noteSetDialog.openDialog()
        this.$refs.noteSetDialog.field = _.cloneDeep(field);
      }else{
        this.$refs.dateSetDialog.openDialog()
        this.$refs.dateSetDialog.field = field;
      }
    },
    // 初始化表单字段
    initCardView() {
      SettingTaskApi.getCardConfig({id: this.taskCardId}).then(res=>{
        const { status, data } = res;
        if(status == 0){
          this.fieldsList = data.cardFieldList;
          this.cardConfig = JSON.parse(data.card.config);
          const { notifyPositionNode = '' } = this.cardConfig
          // 把字符串处理成数组
          this.notifyPositionNode = notifyPositionNode ? notifyPositionNode.split(',') : []
        }
      }).catch(error=>({}))

    },
    // 返回
    back(){
      let fromId = window.frameElement.getAttribute('fromid');
      let id = window.frameElement.dataset.id
      this.$platform.closeTab(id)

      // this.$platform.openTab({
      //   id: 'M_SYSTEM',
      //   title: '附加组件设置',
      //   url: '/setting/task/cardManage',
      //   reload: true,
      //   close: true,
      //   fromId
      // });
      openAccurateTab({
        type: PageRoutesTypeEnum.PageTaskCardManage,
        reload: true,
        fromId
      })
    },
    // 保存工时记录配置
    saveCardConfigReq() {
      let configParams = JSON.parse(JSON.stringify(this.cardConfig));
      configParams.maxTime = configParams.maxTime != '' ? Number(configParams.maxTime) : '';
      const params = {
        cardId: this.taskCardId, 
        config: JSON.stringify(configParams), 
        cardFieldList: this.fieldsList
      }

      SettingTaskApi.saveCardConfig(params).then(res=>{
        const { status, message } = res;
        if(status == 0){
          this.$message.success(i18n.t('task.setting.settingSuccess'))
        }else{
          this.$message.warning(message)
        }
      }).catch(error=>({}))
    },
    // 获取组件名称
    getTaskCardNameReq() {
      SettingTaskApi.getTaskCardName({id: this.taskCardId}).then(res=>{
        const { status, message, data } = res;
        if(status == 0){
          this.cardName = data.name;
        }else{
          this.$message.warning(message);
        }
      }).catch(error=>({}))
    },
  },
  components: {
    [NoteSetDialog.name]: NoteSetDialog,
    [DateSetDialog.name]: DateSetDialog
  },
}